<?php 

class Upload extends CI_Controller {

	// var $iterasi ;
	public function __construct()
	{
		parent::__construct();
		$this->load->library('session');
		$this->load->model('mglobal');
		$this->load->model('mmaster');
		$this->load->model('mupload');
        $this->load->helper('html');        
		$this->load->model('mcluster');
	}


	public function index() 
	{
		$data['template'] = "shell/smooth";
		$data['main_view'] = "f-upload-mfino";
		$data['error'] ="";
		$this->load->view($data['template'], $data);
	}

	public function mfino($mfinoid='', $proses = '') {
		$data['template'] = "shell/smooth";
		$data['main_view'] = "f-upload-mfino";
		// $data['main_view'] = "f-upload-dialog";

		if (!$proses) {
			// $config['upload_path'] = base_url().'file/mfino';
			$config['upload_path'] = './file/mfino';
			$config['file_name'] = date("dmY-Hisa");
			$config['max_size']	= '5000';
			$config['allowed_types'] = 'csv|xls|xlsx|txt';

			$this->load->library('upload', $config);
			$delimiter = $this->input->post('delimiter');

			$eload['updated_by'] = $this->session->userdata('username');
			$eload['created_by'] = $this->session->userdata('username');
			if ( ! $this->upload->do_upload('mfino-csv')) {		// jika tidak berhasil upload data
				$data['error'] = $this->upload->display_errors();
				// echo "terjadi kesalahan saat upload data \n";
			}
			else {												// jika upload data berhasil
				$mfino = $this->upload->data();
				$data['error'] = $this->upload->data();
				$this->db->trans_start(); 					// Save trans
				foreach (glob($mfino['full_path']) as $filename) {
					$file_handle = file($filename);
					$i = 1;
					foreach ($file_handle as $baris) {
						$cols = explode($delimiter, $baris);
						$eload['regional'] = trim($cols[0]);
						$eload['eload_number'] = trim($cols[1]);

						// echo $i++;
						$eload_exist = $this->mupload->getEload($eload['eload_number']);
						if(!$eload_exist) {
							$this->mupload->newEload($eload);	// simpan ke database
						} 
						else {  // Kalau $eload_exist != 0, (kemungkinan = 1), maka.
							$this->mupload->editEload($eload);	// update ke database
						}
					}
				}
				if ($this->db->trans_status() === FALSE)	// Save trans
					$this->db->trans_rollback();			// Save trans
				else
					$this->db->trans_commit();				// Save trans
			}
		} // end of !$process Jika nilai process kosong

		$this->load->view($data['template'], $data);
		// echo '<script language="javascript" type="text/javascript">window.top.window.stopUpload();</script>  ';
	}


	public function eload() {
		$this->redirect('mfino');
	}

	public function kpi() {
		$data['template'] = "shell/smooth";
		$data['main_view'] = "f-upload";

		$this->load->view($data['template'], $data);
	}
    function getTerritory($parent_id='1',$return='return'){
        if ($return=='return')
            return $this->mupload->getTerritory($parent_id);
        else 
            echo json_encode($this->mupload->getTerritory($parent_id));
    }
    
    public function mobile($proses=""){
        $data['template'] = "shell/smooth";
		$data['main_view'] = "f-upload-mobile";
		$data['error'] ="";
        $begining['null']= 'Pilih Region';
        $end= $this->getTerritory();
        $data['region']=$begining;
        $data['region'].=$end;
        $data['cluster']=$begining; 
        if ($proses=="doupload"){
       	    $config['upload_path'] = './file/app/update';
			$config['file_name'] =$this->input->post('app_version');
			//$config['max_size']	= '5000';
			$config['allowed_types'] = 'apk';

			$this->load->library('upload', $config);
		

			$mobile['updated_by'] = $this->session->userdata('username');
			$mobile['created_by'] = $this->session->userdata('username');
			if ( ! $this->upload->do_upload('mobile')) {		// jika tidak berhasil upload data
				$data['error'] = $this->upload->display_errors();
				// echo "terjadi kesalahan saat upload data \n";
			}
			else {												// jika upload data berhasil
				
				//$data['error'] = $this->upload->data();
				$this->db->trans_start(); 					// Save trans
			    $app = $this->upload->data();
                print_r($app);
               // $this->mupload->saveMobileUpdate(); 
				if ($this->db->trans_status() === FALSE)	// Save trans
					$this->db->trans_rollback();			// Save trans
				else
					$this->db->trans_commit();				// Save trans
			}
        }
		$this->load->view($data['template'], $data);
    }

	// function _iterasi() {
	// 	return $iterasi;
	// }
}